﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AO.Core;
using Cell.Core;

namespace ZoneEngine.PacketHandlers
{
    public static class Vicinity
    {
        public static void Read(byte[] Packet, Client cli)
        {
            PacketReader _reader = new PacketReader(Packet);
            _reader.PopShort(); //magic 0xDFDF
            short Type = _reader.PopShort();
            if (Type != 0x0005)
            {
                //TextMessage type
                throw new Exception("Wrong packet type given to VicinityHandler.");
            }
            _reader.PopShort(); //unknown
            short PacketSize = _reader.PopShort();
            int SenderID = _reader.PopInt();

            //5 unknown values
            _reader.PopInt();
            _reader.PopInt();
            _reader.PopInt();
            _reader.PopInt();
            _reader.PopInt();

            short MsgLen = _reader.PopShort();
            string Message = _reader.PopString(MsgLen);

            System.Console.WriteLine("Vicinity: " + Message);

            //now we call the ISC, for sending to chat server :D
            //we need to calculate a list of receivers HERE, but atm, for testing, only ourselves.

            List<Client> Clients = cli.GetClientsInRadius(100.0f);
            int[] recvers = new int[Clients.Count];
            int index = 0;

            foreach (Client child in Clients)
            {
                recvers[index] = child.Character.characterId;
                index++;
            }

            ChatCom.SendVicinity(recvers, Message);
        }
    }
}

